package com.liyongjian.apidoc.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MapUtils {

    public static Map<String, Object> convert(Object c, String... attr) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (c == null) {
            return map;
        }
        Field fields[] = c.getClass().getFields();// 获得对象所有属性
        Field field = null;
        if (attr == null || attr.length == 0) {
            for (int i = 0; i < fields.length; i++) {
                field = fields[i];
                try {
                    map.put(field.getName(), field.get(c));
                } catch (Exception e) {
                }
            }
        } else {
            for (int i = 0; i < fields.length; i++) {
                field = fields[i];
                for (int j = 0; j < attr.length; j++) {
                    if (attr[j].equals(field.getName())) {
                        try {
                            map.put(field.getName(), field.get(c));
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }
        return map;
    }

}
